/**
 * 
 */
package com.nari.resconfig.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.ICity;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.ICityService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.basic.model.IRole;
import com.nari.basic.model.IUser;
import com.nari.basic.service.IRoleService;
import com.nari.basic.utils.sysmgr.UserRoleUtil;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.resconfig.model.IProjectResourceConfig;
import com.nari.resconfig.service.IProjectResourceConfigService;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2014-5-9
 */
public class ProjectResourceConfigAction extends JSONActionSupport {
	private static final long serialVersionUID = 5877089375983314031L;
	private IProjectResourceConfigService iProjectResourceConfigService;
	private IProjectService iProjectService;
	private ICityService iCityService;
	private IRoleService iRoleService;
	// 项目信息查询
	private String projectID;
	private String projectName;
	private String capitalCityID;
	private String provinceCityID;
	private List<ICity> provinceCityList;
	private List<ICity> capitalCityList;
	// 分页查询
	private int pageIndex = 1; // 默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IProject> projectPage;
	private Map<String, String> pageCityMap; // 所属城市id-cityName映射表
	//项目资源配置
	private String projectUID;
	private String authorityRole;
	private String userID;
	private ResultWithPageCond<IUser> userPage;
	private String handleMethod;
	private String userUID;
	private String userUIDList;
	
	/**
	 * 系统管理员对项目资源配置、项目管理员对发卡管理员的项目资源配置
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-9
	 */
	public String intialProjectResAuthority() throws Exception {
		//可携带系统参数
		return "projectResMgr";
	}
	
	/**
	 * 项目资源授权
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-13
	 */
	public String queryProjectListForAuthorize() throws Exception {
		// 行政机构所属关系列表
		this.provinceCityList = this.iCityService.queryFirstLevelCity();
		if (provinceCityID != null && !"".equals(provinceCityID)) {
			this.capitalCityList = this.iCityService.queryCityByParentID(provinceCityID);
		} else {
			this.capitalCityList = new ArrayList<ICity>();
		}
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		// 分页结果
		this.projectPage = this.iProjectService.queryAuthorityProjectWithPageCond(projectID, projectName, 
				capitalCityID, provinceCityID, null, pageCond);
		// 计算结果集的页数
		this.pageSize = new BigDecimal(this.projectPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.projectPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		// 查询项目信息对应的行政机构映射表
		this.pageCityMap = this.iProjectService.queryReferCitysByProjList(projectPage.getList());
		//根据用户角色加载对应的项目资源配置页面
		IUser loginUser = UserRoleUtil.getSysLoginUser();
		//系统管理员的项目资源配置权限
		if(this.superManager.contains(loginUser.getUserID()) || this.loginUserRoleIDList.contains(this.sysadmin)) {
			return "adminProjResList";
		}
		//项目管理员的项目资源配置权限
		if(this.loginUserRoleIDList.contains(this.sysShareManager)) {
			return "managerProjResList";
		}
		return "lackauthority";
	}
	
	/**
	 * 项目资源的用户角色配置页面初始化
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public String initialProjResChargeRole() throws Exception {
		//携带参数：项目唯一标识-projectUID
		IUser loginUser = UserRoleUtil.getSysLoginUser();
		//系统管理员的项目资源配置权限
		if(this.superManager.contains(loginUser.getUserID()) || this.loginUserRoleIDList.contains(this.sysadmin)) {
			return "adminMgrRoleConfig";
		}
		//项目管理员的项目资源配置权限
		if(this.loginUserRoleIDList.contains(this.sysShareManager)) {
			return "managerMgrRoleConfig";
		}
		return "lackauthority";
	}
	
	/**
	 * 通过项目唯一标识查询对应的项目管理员
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public String queryProjectManagersByProjectUID() throws Exception {
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//用户角色
		this.authorityRole = this.sysShareManager;
		this.handleMethod = "queryProjectManagersByProjectUID"; //处理方法缓存
		//分页查询
		this.userPage = iProjectResourceConfigService.queryProjResRoleUser(projectUID, 
				authorityRole, userID, pageCond);
		//计算结果集页数
		this.pageSize = new BigDecimal(this.userPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.userPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		return "projectManagerList";
	}
	
	/**
	 * 通过项目唯一标识查询对应的项目销售人员
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public String queryProjectSalesByProjectUID() throws Exception {
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//用户角色
		this.authorityRole = this.sysSalesman;
		this.handleMethod = "queryProjectSalesByProjectUID"; //处理方法缓存
		//分页查询
		this.userPage = iProjectResourceConfigService.queryProjResRoleUser(projectUID, 
				authorityRole, userID, pageCond);
		//计算结果集页数
		this.pageSize = new BigDecimal(this.userPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.userPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		return "projectSaleList";
	}
	
	/**
	 * 通过项目唯一标识查询对应的项目发卡管理员
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public String queryProejctCardDistributeByProjectUID() throws Exception {
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//用户角色
		this.authorityRole = this.sysCardDistributor;
		this.handleMethod = "queryProejctCardDistributeByProjectUID"; //处理方法缓存
		//分页查询
		this.userPage = iProjectResourceConfigService.queryProjResRoleUser(projectUID, 
				authorityRole, userID, pageCond);
		//计算结果集页数
		this.pageSize = new BigDecimal(this.userPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.userPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		return "projCardDistributeList";
	}
	
	/**
	 * 查询项目资源的可授权用户
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public String queryRoleUserForProjResConfig() throws Exception {
		// 分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页查询
		this.userPage = iProjectResourceConfigService.queryProbablyProjResRoleUser(projectUID, 
				authorityRole, userID, pageCond);
		//计算结果集页数
		this.pageSize = new BigDecimal(this.userPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.userPage.getPageCond().getLimit()), RoundingMode.UP)
				.intValue();
		if (pageSize == 0) pageSize = 1;
		return "roleUserResAuthority";
	}
	
	/**
	 * 取消用户资源授权
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public String cancelAuthorityUser() throws Exception {
		IRole role = iRoleService.queryRoleInfoByRoleID(authorityRole);
		if(role != null) {
			if(this.sysShareManager.equals(authorityRole)) {
				//取消项目管理员
				this.rmsg = iProjectService.deleteProjectManager(projectUID);
				if(rmsg.isSuccess()) {
					this.rmsg = iProjectResourceConfigService.deleteAuthorityUser(projectUID, role.getId(), userUID);
				}
			} else {
				this.rmsg = iProjectResourceConfigService.deleteAuthorityUser(projectUID, role.getId(), userUID);
			}
		} else {
			this.rmsg = this.initLackResponseMsg(authorityRole);
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(this.rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * 校验缺少的角色信息
	 * @param authorityRole
	 * @return
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	private ResponseMsg initLackResponseMsg(String authorityRole) {
		String msg = "缺少编号为【"+authorityRole+"】的";
		if(this.sysadmin.equals(authorityRole)) {
			msg += "【系统管理员】角色信息！";
		}
		if(this.sysShareManager.equals(authorityRole)) {
			msg += "【项目管理员】角色信息！";
		}
		if(this.sysSalesman.equals(authorityRole)) {
			msg += "【销售】角色信息！";
		}
		if(this.sysCardDistributor.equals(authorityRole)) {
			msg += "【发卡管理员】角色信息！";
		}
		return ResponseMsg.getInstance(false, msg);
	}
	
	/**
	 * 对角色用户进行项目资源的批量授权
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-5-19
	 */
	public String saveRoleUserResAuthority() throws Exception {
		IRole role = iRoleService.queryRoleInfoByRoleID(authorityRole);
		if(role != null) {
			if(this.sysShareManager.equals(authorityRole)) {
				this.rmsg = iProjectService.saveProjectManager(projectUID, userUIDList);
				if(rmsg.isSuccess()) {
					List<IProjectResourceConfig> configList = iProjectResourceConfigService.queryProjResByRoleInfo(projectUID, role.getId());
					if(configList!=null && configList.size()>0) {
						IProjectResourceConfig config = configList.get(0);
						config.setUserUID(userUIDList);
						iProjectResourceConfigService.updateEntity(config, config.getId());
						this.rmsg = ResponseMsg.getInstance(true, "");
					} else {
						this.rmsg = iProjectResourceConfigService.saveAuthorityUserBatch(projectUID, role.getId(), userUIDList);
					}
				}
			} else {
				this.rmsg = iProjectResourceConfigService.saveAuthorityUserBatch(projectUID, role.getId(), userUIDList);
			}
		} else {
			this.rmsg = this.initLackResponseMsg(authorityRole);
		}
		this.rmsg = ResponseMsg.getInstance(true, "");
		this.writeJsonStrToResponse(JSONUtil.serialize(rmsg));
		return Action.SUCCESS;
	}
	
	/**
	 * @return the iProjectResourceConfigService
	 */
	@JSON(serialize=false)
	public IProjectResourceConfigService getiProjectResourceConfigService() {
		return iProjectResourceConfigService;
	}

	/**
	 * @param iProjectResourceConfigService the iProjectResourceConfigService to set
	 */
	public void setiProjectResourceConfigService(
			IProjectResourceConfigService iProjectResourceConfigService) {
		this.iProjectResourceConfigService = iProjectResourceConfigService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCityService
	 */
	@JSON(serialize=false)
	public ICityService getiCityService() {
		return iCityService;
	}

	/**
	 * @param iCityService the iCityService to set
	 */
	public void setiCityService(ICityService iCityService) {
		this.iCityService = iCityService;
	}

	/**
	 * @return the iRoleService
	 */
	@JSON(serialize=false)
	public IRoleService getiRoleService() {
		return iRoleService;
	}

	/**
	 * @param iRoleService the iRoleService to set
	 */
	public void setiRoleService(IRoleService iRoleService) {
		this.iRoleService = iRoleService;
	}

	/**
	 * @return the projectID
	 */
	public String getProjectID() {
		return projectID;
	}

	/**
	 * @param projectID the projectID to set
	 */
	public void setProjectID(String projectID) {
		this.projectID = projectID;
	}

	/**
	 * @return the projectName
	 */
	public String getProjectName() {
		return projectName;
	}

	/**
	 * @param projectName the projectName to set
	 */
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	/**
	 * @return the capitalCityID
	 */
	public String getCapitalCityID() {
		return capitalCityID;
	}

	/**
	 * @param capitalCityID the capitalCityID to set
	 */
	public void setCapitalCityID(String capitalCityID) {
		this.capitalCityID = capitalCityID;
	}

	/**
	 * @return the provinceCityID
	 */
	public String getProvinceCityID() {
		return provinceCityID;
	}

	/**
	 * @param provinceCityID the provinceCityID to set
	 */
	public void setProvinceCityID(String provinceCityID) {
		this.provinceCityID = provinceCityID;
	}

	/**
	 * @return the provinceCityList
	 */
	public List<ICity> getProvinceCityList() {
		return provinceCityList;
	}

	/**
	 * @param provinceCityList the provinceCityList to set
	 */
	public void setProvinceCityList(List<ICity> provinceCityList) {
		this.provinceCityList = provinceCityList;
	}

	/**
	 * @return the capitalCityList
	 */
	public List<ICity> getCapitalCityList() {
		return capitalCityList;
	}

	/**
	 * @param capitalCityList the capitalCityList to set
	 */
	public void setCapitalCityList(List<ICity> capitalCityList) {
		this.capitalCityList = capitalCityList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the projectPage
	 */
	public ResultWithPageCond<IProject> getProjectPage() {
		return projectPage;
	}

	/**
	 * @param projectPage the projectPage to set
	 */
	public void setProjectPage(ResultWithPageCond<IProject> projectPage) {
		this.projectPage = projectPage;
	}

	/**
	 * @return the pageCityMap
	 */
	public Map<String, String> getPageCityMap() {
		return pageCityMap;
	}

	/**
	 * @param pageCityMap the pageCityMap to set
	 */
	public void setPageCityMap(Map<String, String> pageCityMap) {
		this.pageCityMap = pageCityMap;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the authorityRole
	 */
	public String getAuthorityRole() {
		return authorityRole;
	}

	/**
	 * @param authorityRole the authorityRole to set
	 */
	public void setAuthorityRole(String authorityRole) {
		this.authorityRole = authorityRole;
	}

	/**
	 * @return the userID
	 */
	public String getUserID() {
		return userID;
	}

	/**
	 * @param userID the userID to set
	 */
	public void setUserID(String userID) {
		this.userID = userID;
	}

	/**
	 * @return the userPage
	 */
	public ResultWithPageCond<IUser> getUserPage() {
		return userPage;
	}

	/**
	 * @param userPage the userPage to set
	 */
	public void setUserPage(ResultWithPageCond<IUser> userPage) {
		this.userPage = userPage;
	}

	/**
	 * @return the handleMethod
	 */
	public String getHandleMethod() {
		return handleMethod;
	}

	/**
	 * @param handleMethod the handleMethod to set
	 */
	public void setHandleMethod(String handleMethod) {
		this.handleMethod = handleMethod;
	}

	/**
	 * @return the userUID
	 */
	public String getUserUID() {
		return userUID;
	}

	/**
	 * @param userUID the userUID to set
	 */
	public void setUserUID(String userUID) {
		this.userUID = userUID;
	}

	/**
	 * @return the userUIDList
	 */
	public String getUserUIDList() {
		return userUIDList;
	}

	/**
	 * @param userUIDList the userUIDList to set
	 */
	public void setUserUIDList(String userUIDList) {
		this.userUIDList = userUIDList;
	}

}
